package day04;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class Test01 {
    public static void main(String[] args) {
        homeworkOne();
        homeworkTwo();
    }

    private static void homeworkTwo() {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            int randomNum = (int) (Math.random() * 21);
            if (list.contains(randomNum)) {
                i--;
                continue;
            }
            list.add(randomNum);
        }
        System.out.println(list);
    }

    private static void homeworkOne() {
        List<String> list = new ArrayList<>();
        list.add("aaa");
        list.add("aaa");
        list.add("bbb");
        list.add("ccc");
        list.add("ddd");
        list.add("aaa");
        list.add("ccc");
        System.out.println(list); //[aaa, aaa, bbb, ccc, ddd, aaa, ccc]
        list.sort(Comparator.comparingInt(String::hashCode));
        System.out.println(list); //[aaa, aaa, aaa, bbb, ccc, ccc, ddd]
        /*
         * 原地去重
         * 为了这包醋才有这炖饺子
         * */
        int fast = 0;
        int slow = 0;
        while (fast < list.size()) {
            if (!list.get(fast).equals(list.get(slow))) {
                slow++;
                list.set(slow, list.get(fast));
            }
            fast++;
        }
        System.out.println(list.subList(0, slow + 1));
    }
}
